﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LPV_POS.Module;

namespace LPV_POS.mh_pos
{
    public partial class mh_banhang9_NhapTien_The : Form
    {
        public mh_banhang9_NhapTien_The()
        {//409, 225
            InitializeComponent();
        }

        
        private void mh_banhang6_PhuThu_Load(object sender, EventArgs e)
        {
            DataTable tbDM_THE_TRA_TRUOC = null;
            tbDM_THE_TRA_TRUOC = new thuvien().SP_SelectDataTable("sp_DM_THE_TRA_TRUOC", 
                                                new string[] { "@flag" }, 
                                                new object[] { 4 });
            grdDM_THE_TRA_TRUOC.DataSource = tbDM_THE_TRA_TRUOC;
        }

     
        private void lblTheTraTruoc_Click(object sender, EventArgs e)
        {
        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {//Nap Tien
            int iDM_THE_TRA_TRUOC_ID = -1;
            float itien = 0;
            if (grvDM_THE_TRA_TRUOC.FocusedRowHandle < 0)
                goto here;
            DataRow dr = grvDM_THE_TRA_TRUOC.GetDataRow(grvDM_THE_TRA_TRUOC.FocusedRowHandle);
            if (dr != null)
            {
                int.TryParse(dr["DM_THE_TRA_TRUOC_ID"] + "", out  iDM_THE_TRA_TRUOC_ID);
                if (iDM_THE_TRA_TRUOC_ID == 0)
                    iDM_THE_TRA_TRUOC_ID = -1;
                float.TryParse(dr["tien"] + "", out itien);
            }
            //
            here:
            if (iDM_THE_TRA_TRUOC_ID <= 0)
            {
                MessageBox_LPV.Show("Chọn thẻ cần nạp tiền");
            }
            else
            {
                thuvien othuvien = new thuvien();
                othuvien.SP_Update("sp_DM_THE_TRA_TRUOC_NOP_TIEN",
                                new string[] { "@flag", "@DM_THE_TRA_TRUOC_ID", "@TIEN", "@TIEN_SAU" },
                                new object[] { 1, iDM_THE_TRA_TRUOC_ID, nudTien_Nap.Value, itien + (float)nudTien_Nap.Value }
                                );
                this.grvDM_THE_TRA_TRUOC_FocusedRowChanged(null, null);

            }
        }

        private void calcEdit2_EditValueChanged(object sender, EventArgs e)
        {
            
        }
        //int iDM_THE_TRA_TRUOC_ID = -1;

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            //iDM_THE_TRA_TRUOC_ID = -1;

            DataTable tbDM_THE_TRA_TRUOC = null;
            tbDM_THE_TRA_TRUOC = new thuvien().SP_SelectDataTable("sp_DM_THE_TRA_TRUOC",
                                                new string[] { "@flag", "@DM_THE_TRA_TRUOC_CODE" },
                                                new object[] { 4, calcEdit2.Value });
            grdDM_THE_TRA_TRUOC.DataSource = tbDM_THE_TRA_TRUOC;
            this.grvDM_THE_TRA_TRUOC_FocusedRowChanged(null, null);
        }

        private void grvDM_THE_TRA_TRUOC_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow dr = grvDM_THE_TRA_TRUOC.GetDataRow(grvDM_THE_TRA_TRUOC.FocusedRowHandle);
            int iDM_THE_TRA_TRUOC_ID = -1;
            if (dr != null)
            {
                int.TryParse(dr["DM_THE_TRA_TRUOC_ID"] + "", out  iDM_THE_TRA_TRUOC_ID);
                if (iDM_THE_TRA_TRUOC_ID == 0)
                    iDM_THE_TRA_TRUOC_ID = -1;
            }
            thuvien othuvien = new thuvien();
            DataTable tbDM_NAP_TIEN = othuvien.SP_SelectDataTable("sp_DM_THE_TRA_TRUOC_NOP_TIEN",
                                                    new string[] { "@flag", "@DM_THE_TRA_TRUOC_ID" },
                                                    new object[] { 4, iDM_THE_TRA_TRUOC_ID });
            grdDM_NAP_TIEN.DataSource = tbDM_NAP_TIEN;
        }

        private void btnTheMoi_Click(object sender, EventArgs e)
        {
            if (nudTheMoi_Ma.Value <= 0)
            {
                MessageBox_LPV.Show("Nhập mã thẻ");
                return;
            }
            if (nudTheMoi_Tien.Value <= 0)
            {
                MessageBox_LPV.Show("Nhập tiền");
                return;
            }
            //
            int irt = 0;
            thuvien othuvien = new thuvien();
            for (int i = 0; i < nud_TongSoThe.Value; i++)
            {
                irt += othuvien.SP_Update("sp_DM_THE_TRA_TRUOC",
                                new string[] { "@flag", "@DM_THE_TRA_TRUOC_CODE", "@TIEN" },
                                new object[] { 1, nudTheMoi_Ma.Value + i, nudTheMoi_Tien.Value }
                                );
            }
            //
            if (irt > 0)
            {
                DataTable tbDM_THE_TRA_TRUOC = null;
                tbDM_THE_TRA_TRUOC = new thuvien().SP_SelectDataTable("sp_DM_THE_TRA_TRUOC",
                                                    new string[] { "@flag" },
                                                    new object[] { 4 });
                grdDM_THE_TRA_TRUOC.DataSource = tbDM_THE_TRA_TRUOC;

                nudTheMoi_Ma.Value = 0;
                nudTheMoi_Tien.Value = 0;
                nud_TongSoThe.Value = 1;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnExcel_DSThe_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            string sfile = Application.StartupPath + "\\DS_The_Tra_Truoc_" + Variable.ngay_gio_hienhanh.ToString("yyyyMMdd_HHmmss") + ".xls";
            grvDM_THE_TRA_TRUOC.ExportToExcelOld(sfile);
            if (System.IO.File.Exists(sfile))
                System.Diagnostics.Process.Start(sfile);
            this.Cursor = Cursors.Default;
        }
    }
}
